//
//  YDYMeFooterView.m
//  DYSisterAPP
//
//  Created by 杨德勇 on 16/7/31.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYMeFooterView.h"
#import "YDYHTTPSessionManager.h"
#import "YDYMeSquareModel.h"
#import <MJExtension.h>
#import "YDYSquareBtn.h"
#import "YDYWebViewController.h"

@implementation YDYMeFooterView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        NSString *url = @"http://api.budejie.com/api/api_open.php";
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"a"] =@"square";
        params[@"c"] =@"topic";
        
        
        [[YDYHTTPSessionManager manager] GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            YDYLog(@"为什么会来这里");
        } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            NSArray *squares = [YDYMeSquareModel mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
            
            [self creartSquare:squares];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            YDYLog(@"图片下载的出错啦%@",error);
        }];
        
    }
    
    
    return self;
}


-(void)creartSquare:(NSArray *)squares{
    
    
    NSInteger count  = squares.count;
    int maxCol = 4;//一行的列数
    CGFloat btnW = self.ydy_width / maxCol;
    CGFloat btnH = btnW;
    
    for (int i = 0; i < count; i++) {
        
        
      
        YDYMeSquareModel *square = squares[i];
        
        YDYSquareBtn *btn = [YDYSquareBtn buttonWithType:UIButtonTypeCustom];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        btn.ydy_x = (i % maxCol) * btnW;
        btn.ydy_y = (i / maxCol) * btnH;
        //设置按钮间的线条
        btn.ydy_width  = btnW - 2;
        btn.ydy_height = btnH - 2;
        
        btn.squareModel = square;
        btn.tag = i;
    }
    
    //设置整个view高度
    self.ydy_height = self.subviews.lastObject.ydy_bottom;
    
    //设置tableview的高度
    UITableView *tableview = (UITableView *)self.superview;
    //设置高度之后，一定要立刻加进去，再重新加载内容,不然切去别地地方就会出问题
    tableview.tableFooterView = self;
//    tableview.contentSize = CGSizeMake(0, self.ydy_bottom);这样写滑回来又出bug了
    [tableview reloadData];
    
}

-(void)btnClick:(YDYSquareBtn *) btn{
    
    
    if ([btn.squareModel.url hasPrefix:@"http"]) {
        //初始化view
        YDYWebViewController *webview = [[YDYWebViewController alloc]init];
        webview.url = btn.squareModel.url;
        webview.navigationController.title = btn.squareModel.name;
        //拿到控制器
        UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
        UINavigationController *nav = [tab selectedViewController];
        [nav pushViewController:webview animated:YES];
        
    }else{
        YDYLog(@"点击的按钮是公司内部的");
    }
    
}


@end
